<?php
namespace App\Modules\Relationship\Models;

use App\Modules\Relationship\Interfaces\ModelInterface;
use App\Http\Services\CommonServices;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Base;

class SweetExtendPositionModel extends Base{

    const DEFAULT_POSITION_NUM = 3;
    const SWEET_POSITION_DIAMOND_NUM = 999; // 购买你有扩展位置价格
    protected $table = "rs_sweet_extend_position";
    protected $titleTable = "rs_sweet_user_title";
    protected $fields = ['id', 'user_id as userId', 'num'];
    protected $tablePrefix = '';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
        $this->tablePrefix = env('DB_PREFIX');
    }

    // 查询密友位个数
    public function getPositionNum(int $userId):int {
        $re = $this->getPositionReCount($userId);
        if ($re) {
            return $re['num'];
        }
        return self::DEFAULT_POSITION_NUM;
    }

    // 查询扩展表中有没有记录
    public function getPositionReCount (int $userId) {
        $condition = array(
            'user_id'=>$userId,
            'is_deleted'=>0
        );
        $re = $this->getOneByCondition($condition, $this->fields);
        if (!$re) {
            return null;
        }
        return $re;
    }

    public function updatePositionNum (int $id) {
        return DB::table($this->table)->where('id', $id)->increment("num");
    }

    public function insertPosition ($userId) {
        return DB::table($this->table)->insert([
           "user_id"=>$userId,
           "num"=>4
        ]);
    }
}

